Completed
Push — master ( 309a9d...83e30f )
by Mathieu
15s queued 12s
created

GetFairCalendarOverview.calculateNumberOfMealTicketsByDate   A

Complexity

Conditions 5

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
dl 0
loc 19
rs 9.1832
c 0
b 0
f 0
1
import { Inject } from '@nestjs/common';
2
import { FairCalendarView } from 'src/Application/FairCalendar/View/FairCalendarView';
3
import { CooperativeNotFoundException } from '../Settings/Repository/CooperativeNotFoundException';
4
import { ICooperativeRepository } from '../Settings/Repository/ICooperativeRepository';
5
import { ICalendarOverview } from './ICalendarOverview';
6
7
export class GetFairCalendarOverview {
8
  constructor(
9
    @Inject('ICooperativeRepository')
10
    private readonly cooperativeRepository: ICooperativeRepository
11
  ) {}
12
13
  public async index(items: FairCalendarView[]): Promise<ICalendarOverview> {
14
    const cooperative = await this.cooperativeRepository.find();
15
    if (!cooperative) {
16
      throw new CooperativeNotFoundException();
17
    }
18
19
    const overviewInDays: ICalendarOverview = {
20
      mission: 0,
21
      dojo: 0,
22
      formationConference: 0,
23
      leave: 0,
24
      support: 0,
25
      other: 0
26
    };
27
28
    for (const { time, type: itemType, project } of items) {
29
      const type = itemType.startsWith('leave_') ? 'leave' : itemType;
30
      const dayDuration = project ? project.dayDuration : cooperative.getDayDuration();
31
      const days = time / dayDuration;
32
33
      overviewInDays[type] = Math.round((overviewInDays[type] + days) * 100) / 100;
34
    }
35
36
    return overviewInDays;
37
  }
38
}
39